package com.glodon.jcloud.increment.shorturl;

import cn.hutool.core.util.StrUtil;
import com.glodon.jcloud.common.utils.Converter;
import com.glodon.jcloud.increment.common.cache.CommonCacheOperator;
import com.glodon.jcloud.increment.shorturl.filter.BasicAuthFilter;
import com.glodon.jcloud.increment.shorturl.service.OAuth2ClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Create Date: 2022/7/4
 *
 * @description 广联达 - 增值业务
 * @version: V8.3.4 -SNAPSHOT
 * @smsor: Terry.Li
 * @mail: lib-o@glodon.com
 */
@Slf4j
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.glodon.jcloud.increment.rpc")
@EnableHystrix
@RestController
@RequestMapping
@SpringBootApplication(scanBasePackages = {"com.glodon.jcloud.increment.shorturl", "com.glodon.jcloud.increment.rpc", "com.glodon.jcloud.signature.utils"})
public class ShortUrlApplication extends SpringBootServletInitializer {

    /**
     * * The Context.
     */
    static ConfigurableApplicationContext context;

    static {
        System.setProperty("pagehelper.banner", "false");
    }

    /**
     * Configure spring application builder.
     *
     * @param application the application
     * @return the spring application builder
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ShortUrlApplication.class);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public CommonCacheOperator cache() {
        return new CommonCacheOperator();
    }


    /**
     * Health string.
     *
     * @return the string
     */
    @GetMapping("/health")
    public String health() {
        return "ok";
    }

    /**
     * * The entry point of application.
     *
     * @param args the input arguments
     * @throws UnknownHostException the unknown host exception
     */
    public static void main(String[] args) throws UnknownHostException {
        SpringApplication springApplication = new SpringApplication(ShortUrlApplication.class);
        springApplication.setBannerMode(Banner.Mode.OFF);
        springApplication.setAllowCircularReferences(Boolean.TRUE);
        context = springApplication.run(args);
        Environment env = context.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = Converter.getString(env.getProperty("server.servlet.context-path"));
        log.info("\n----------------------------------------------------------\n" +
                "Application Api Service is running! Access URLs:\n" +
                "Local: \t\t\t\thttp://localhost:" + port + path + "\n" +
                "External: \t\t\thttp://" + ip + ":" + port + path + "\n" +
                "Swagger文档: \t\thttp://" + ip + ":" + port + path + "doc.html\n" +
                "----------------------------------------------------------");
        log.info(">>> {}", ShortUrlApplication.class.getSimpleName().toUpperCase() + " STARTING SUCCESS");
    }

}
